package o2o.web.actions.shop;

import com.jinyou.utils.common.*;
import o2o.cache.agent.ShopAgentRelationMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.shop.SHOP_IS_INTEGRAL;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.agent.CityAgentVO;
import o2o.system.data.SHOP_MANAGE_TYPE_FREE_OR_AGENT;
import o2o.system.data.SYS_SETTINGS;
import o2o.web.actions.agent.CityAgentMan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class ShopList2Action {

    // 获取直邮店铺列表
    @RequestMapping(method = RequestMethod.POST, value = {"/user/zhiyou/shop/list", "/admin/zhiyou/shop/list"})
    @ResponseBody
    public String getAllShop2(String sysAppKey, String token, String city, String county, Double lat, Double lng,
                              Long agentId,//精确商圈代理Id
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "20") Integer size) {

        UserInfo user = null;
        if (ValidateUtil.isNotNull(token)) {
            user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        }

        List<ShopInfo> list = null;
        if (ValidateUtil.isID(agentId)) {
            // 首先查出所有的商品来
            List<ShopInfo> tmpList = ShopMan.listOfAgentId(agentId, "");
            list = checkShopList(tmpList, null, null);
        } else if (ValidateUtil.isLatLng(lat, lng)) {
            //精确商圈方式
            if (null != SYS_SETTINGS.SHOP_MANAGE_TYPE && 0 == SHOP_MANAGE_TYPE_FREE_OR_AGENT.AGENT - SYS_SETTINGS.SHOP_MANAGE_TYPE) {
                List<Long> shopIds = new ArrayList<>();
                //单商圈还是多商圈同时返回
                if (null != SYS_SETTINGS.LOCAL_CITY_AGENT_TYPE && 0 == SYS_SETTINGS.LOCAL_CITY_AGENT_TYPE) { //定位时城市按下商圈列表还是单独商圈  1单独商圈，0商圈列表
                    //商圈列表
                    List<CityAgentVO> agentList = CityAgentMan.getCityAgentList(city, lat, lng);
                    if (ValidateUtil.isAbsList(agentList)) {
                        list = new ArrayList<>();
                        for (int i = 0; i < agentList.size(); i++) {
                            CityAgentVO agentVO = agentList.get(i);
                            if (null == agentVO) {
                                continue;
                            }
                            List<Long> tmpList = ShopAgentRelationMemory.getInstance().listOfAgentId(agentVO.getId());
                            if (!ValidateUtil.isAbsList(tmpList)) {
                                continue;
                            }
                            List<ShopInfo> tList = convertShopList(agentVO, tmpList, lat, lng);
                            if (ValidateUtil.isAbsList(tList)) {
                                list.addAll(tList);
                            }
                        }
                    }

                } else {
                    //单独商圈
                    CityAgentVO cityAgentVO = CityAgentMan.getCityAgent(city, lat, lng);
                    if (null != cityAgentVO) {
                        shopIds = ShopAgentRelationMemory.getInstance().listOfAgentId(cityAgentVO.getId());
                        if (ValidateUtil.isAbsList(shopIds)) {
                            list = convertShopList(cityAgentVO, shopIds, lat, lng);
                        }
                    }
                }

                // 所在商圈没有的话，获取城市里的
                if (!ValidateUtil.isAbsList(list)) {
                    // 所在商圈没有的话，获取城市里的
                    List<ShopInfo> tmpList = ShopMemory.getInstance().getCityShopList(city, county, "", null);//该城市店铺列表  管理后台可能用到
                    list = checkShopList(tmpList, lat, lng);
                }
            } else {
                Long areaPoint = Geohash.getArea(lat, lng);
                int range = 15;//大概是10公里
                if (ValidateUtil.isAbsInteger(SYS_SETTINGS.SHOP_RANGE)) {
                    range = SYS_SETTINGS.SHOP_RANGE;
                }
                Set<Long> areaPoints = Geohash.getAroundArea(areaPoint, range);

                List<ShopInfo> tmp = ShopMemory.getInstance().getCityShopList(city, county, "", null);//该城市店铺列表
                if (ValidateUtil.isAbsList(tmp)) {
                    list = new ArrayList<>();
                    for (int i = 0; i < tmp.size(); i++) {
                        ShopInfo shopInfo = tmp.get(i);
                        if (null == shopInfo) {
                            continue;
                        }
                        if (ValidateUtil.isNotLatLng(lat, lng)) {
                            continue;
                        }
                        if (ValidateUtil.isNotAbsLong(shopInfo.getAreaPoint())) {
                            continue;
                        }
                        if (!areaPoints.contains(shopInfo.getAreaPoint())) {
                            continue;
                        }
                        if (shopInfo.getIsIntegralShop() == null
                                || shopInfo.getIsIntegralShop() == SHOP_IS_INTEGRAL.NON_INTEGRAL_SHOP || shopInfo.getIsIntegralShop() == SHOP_IS_INTEGRAL.BOTH_SHOP) {
                        } else {
                            // 积分商城店铺
                            continue;
                        }
                        if (null != shopInfo.getCanZhiYou() && 0 == 1 - shopInfo.getCanZhiYou()) {
                            // 直邮店铺
                        } else {
                            continue;
                        }

                        shopInfo.setDistinct(Geohash.distance(lat, lng, shopInfo.getLat(), shopInfo.getLng()));
                        list.add(shopInfo);
                    }
                }

            }

            // 都没有  获取所有的
            if (!ValidateUtil.isAbsList(list)) {
                // 所在商圈没有的话，获取城市里的
                List<ShopInfo> tmpList = ShopMemory.getInstance().getCityShopList("", "", "", null);//该城市店铺列表  管理后台可能用到
                list = checkShopList(tmpList, lat, lng);
            }

            // 距离近的放前面
            if (ValidateUtil.isAbsList(list)) {
                Comparator<ShopInfo> work = Comparator.comparingInt(ShopInfo::getIsWork).reversed();
                Collections.sort(list, work.thenComparing(Comparator.comparingDouble(ShopInfo::getDistinct)));
            }
        } else {
            List<ShopInfo> tmpList = ShopMemory.getInstance().getCityShopList(city, county, "", null);//该城市店铺列表  管理后台可能用到
            list = checkShopList(tmpList, null, null);
        }

        // 分页
        if (ValidateUtil.isAbsList(list)) {
            if (null == page || page < 1) {
                page = 1;
            }
            if (null == size || size < 1) {
                size = 10;
            }

            JYPageUtils.PageList pageList = JYPageUtils.getList(list, page, size);
            if (null != pageList) {
                //            for (ShopInfoAndGameVO shopInfoAndGameVO : (List<ShopInfoAndGameVO>) pageList.list) {
//                ShopListMan.putShopGameInfo(user, shopInfoAndGameVO, true);
//            }
                return AjaxResult.successResult(pageList.totalCount, pageList.size, pageList.list);
            }
        }

        return AjaxResult.successResult(0, 0, null);
    }

    //店铺列表信息
    public static List<ShopInfo> convertShopList(CityAgentVO cityAgentVO,
                                                 List<Long> shopIds,
                                                 Double lat, Double lng) {
        if (null == cityAgentVO) {
            return null;
        }

        if (ValidateUtil.isAbsList(shopIds)) {

            boolean userAgentYunfei = false;//使用商圈的运费规则
            if (null != SYS_SETTINGS.SELECT_DISTRIBUTION_FEE && 0 == 3 - SYS_SETTINGS.SELECT_DISTRIBUTION_FEE) {
                userAgentYunfei = true;
            }

            List<ShopInfo> list = new ArrayList<>();
            for (int i = 0; i < shopIds.size(); i++) {
                if (null == shopIds.get(i)) {
                    continue;
                }
                ShopInfo tmpShop = ShopMemory.getInstance().getShopById(shopIds.get(i));
                if (null == tmpShop) {
                    continue;
                }
                ShopInfo shopInfo = new ShopInfo();
                try {
                    ObjectUtils.copyPropertiesExclude(tmpShop, shopInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                if (null == shopInfo) {
                    continue;
                }

                if (shopInfo.getIsIntegralShop() == null
                        || shopInfo.getIsIntegralShop() == SHOP_IS_INTEGRAL.NON_INTEGRAL_SHOP || shopInfo.getIsIntegralShop() == SHOP_IS_INTEGRAL.BOTH_SHOP) {
                } else {
                    // 积分商城店铺
                    continue;
                }
                if (null != shopInfo.getCanZhiYou() && 0 == 1 - shopInfo.getCanZhiYou()) {
                    // 直邮店铺
                } else {
                    continue;
                }
                shopInfo.setDistinct(Geohash.distance(lat, lng, shopInfo.getLat(), shopInfo.getLng()));

                if (userAgentYunfei && ValidateUtil.isNotAbsDouble(shopInfo.getFixedCost()) && ValidateUtil.isNotAbsDouble(shopInfo.getWithinDistance()) && ValidateUtil.isNotAbsDouble(shopInfo.getOneKmCost())) {
                    shopInfo.setFixedCost(cityAgentVO.getFixedCost());
                    shopInfo.setWithinDistance(cityAgentVO.getWithinDistance());
                    shopInfo.setOneKmCost(cityAgentVO.getOneKmCost());
                }
                list.add(shopInfo);
            }
            return list;
        }
        return null;
    }

    public static List<ShopInfo> checkShopList(List<ShopInfo> list, Double lat, Double lng) {
        if (ValidateUtil.isAbsList(list)) {
            List reList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                ShopInfo shopInfo = list.get(i);
                if (null == shopInfo) {
                    continue;
                }

                if (ValidateUtil.isNotAbsLong(shopInfo.getAreaPoint())) {
                    continue;
                }

                if (shopInfo.getIsIntegralShop() == null
                        || shopInfo.getIsIntegralShop() == SHOP_IS_INTEGRAL.NON_INTEGRAL_SHOP || shopInfo.getIsIntegralShop() == SHOP_IS_INTEGRAL.BOTH_SHOP) {
                } else {
                    // 积分商城店铺
                    continue;
                }
                if (null != shopInfo.getCanZhiYou() && 0 == 1 - shopInfo.getCanZhiYou()) {
                    // 直邮店铺
                } else {
                    continue;
                }
                if (ValidateUtil.isLatLng(lat, lng) && ValidateUtil.isLatLng(shopInfo.getLat(), shopInfo.getLng())) {
                    shopInfo.setDistinct(Geohash.distance(lat, lng, shopInfo.getLat(), shopInfo.getLng()));
                }

                reList.add(shopInfo);
            }
            return reList;
        }
        return null;
    }

}
